package com.sikaryofficial.canalclient.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sikaryofficial.canalclient.domain.doc.ActivityDoc;
import com.sikaryofficial.canalclient.domain.entity.ActivityMP;
import com.sikaryofficial.canalclient.domain.mapping.ActivityMapping;
import com.sikaryofficial.canalclient.eemapper.ActivityEeMapper;
import com.sikaryofficial.canalclient.service.IActivityService;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 测试使用Easy-ES
 * <p>
 * Copyright © 2021 xpc1024 All Rights Reserved
 **/
@RestController
@Slf4j
public class ActivityEeController {
    @Autowired
    private ActivityEeMapper activityEeMapper;

    @Autowired
    private IActivityService activityService;

    @GetMapping("/createIndex")
    public Boolean createIndex() {
        // 1.初始化-> 创建索引(相当于mysql中的表)
        if(!activityEeMapper.existsIndex("activity_t")){
            activityEeMapper.createIndex();
        }
        return true;
    }
    @Value("${hismk.store.limitCount:20000}")
    private Integer limitQueryStoreCount;

    @GetMapping("/init")
    public Boolean init() {
        // 1.初始化-> 创建索引(相当于mysql中的表)
        int storedCount = activityService.count(new LambdaQueryWrapper<ActivityMP>()
                .eq(ActivityMP::getDeletedVersion,0L));
        log.info("count:{}",storedCount);
       // 限制查询数据量
        if (storedCount > limitQueryStoreCount) {
            storedCount = limitQueryStoreCount;
            // 分批查询数据
            int pageSize = 2000;
            int startPage ;
            for (int i = 0; i < storedCount / pageSize; i++) {
                LambdaQueryWrapper<ActivityMP> query = new LambdaQueryWrapper<>();
                query.eq(ActivityMP::getDeletedVersion, 0L);
                query.orderByDesc(ActivityMP::getActivityId);
                startPage = i * pageSize;
                // 第startPage条记录开始,每页取 pageSize 记录
                query.last(" limit " + startPage + "," + pageSize);
                List<ActivityMP> storeList =  activityService.list(query);
                activityEeMapper.insertBatch(ActivityMapping.INSTANCE.coverPoToList(storeList));
            }
        }else{
            List<ActivityMP> storeList =  activityService.list(new LambdaQueryWrapper<ActivityMP>()
                    .eq(ActivityMP::getDeletedVersion, 0L)
                    .orderByDesc(ActivityMP::getActivityId)
            );
            activityEeMapper.insertBatch(ActivityMapping.INSTANCE.coverPoToList(storeList));
        }
        return true;
    }

    @GetMapping("/insert")
    public Integer insert() {
        // 2.初始化-> 新增数据
        activityEeMapper.insert(ActivityDoc.builder()
                        .activityId(11111L)
                        .activityName("活动"+ new Random().nextInt(1000))
                        .activityDesc("活动描述"+ new Random().nextInt(1000))
                .build());
        return 1;
    }

    @GetMapping("/search")
    public List<ActivityDoc> search() {
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<ActivityDoc> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(ActivityDoc::getActivityDesc, "老汉");
        return activityEeMapper.selectList(wrapper);
    }
}
